package com.example.app1

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.util.AttributeSet
import android.view.View

class TestView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val bitmap = getBitmap()
        val bitmap2 = getBitmap()
val layer = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null)
//        canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
        canvas.drawBitmap(bitmap2, 0f, 0f, paint)
        paint.xfermode = xfermode
        canvas.drawBitmap(bitmap, 100.dp, 100.dp, paint)
        paint.xfermode = null
        canvas.restoreToCount(layer)
    }

    private fun getBitmap(): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeResource(resources, R.drawable.abc, options)
        options.inDensity = options.outWidth
        options.inTargetDensity = 200.dp.toInt()
        options.inJustDecodeBounds = false
        return BitmapFactory.decodeResource(resources, R.drawable.abc, options)
    }

    private fun getItemBackground(width: Int, height: Int, radius: Float): Bitmap {
        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        canvas.drawRoundRect(0f, 0f, width.toFloat(), height.toFloat(), radius, radius, paint)
        canvas.drawRect(0f, 0f, radius, radius, paint)
        canvas.drawRect(width - radius, 0f, width.toFloat(), radius, paint)
        return bitmap
    }
}